import type { Level, hotwords } from '#/request'
import { requestChannel, requestHotKey } from '@/service'
import { useChannelStore } from '@/store/channel'
import { ref } from 'vue'
async function useChannel(configId) {
  const hotWords = ref<hotwords[]>([])
  const { data }: any = await requestChannel({
    configId,
  })
  const systemValue = {
    // 转人工按钮是否展示
    openTurnLabor: Boolean(Number(data.basicSetting.isTurnLabor)),
    // 是否开启转人工
    turnLaborAction: Boolean(Number(data.basicSetting.isCanTurnLabor)),
    // 是否开启访客留言
    visitorLeave: Boolean(Number(data.basicSetting.visitorLeaveMessage)),
    //是否开启机器人
    openRobot: Boolean(Number(data.basicSetting.isRobot)),
    //背景图
    backgroundImage: data.basicSetting.backGroundUrl,
    // 浏览器标题
    browserTitle: data.basicSetting.title,
    // 是否开启强认证
    isStrongAuth: Boolean(Number(data.basicSetting.isMustAuthentication)),
    // 拒识转人工次数
    refuseTransferLabor: parseInt(
      data.systemConfiguration.ics_intervalDuration,
    ),
    // 访客头像
    visitorAvatar: data.systemConfiguration.default_visitor_img_url,
    // 坐席头像
    agentAvatar: data.systemConfiguration.default_seat_img_url,
    // 机器人头像
    robotAvatar: data.systemConfiguration.default_robot_img_url,
    // 渠道欢迎语
    channelWelcome: data.basicSetting.channelWelcomeMsg,
    // 渠道结束语
    channelEnding: data.basicSetting.channelCloseMsg,
    robotChannelId: data.basicSetting.robotChannelId,
    tenantId: data.entranceDTO.tenantId,
    // 机器人欢迎语
    robotWelcome: data.basicSetting.enableWelcomeMessage,
    // 是否可以主动评价
    evaluateAction: Boolean(Number(data.satisfactionDTO.visitorInitiative)),
    // 是否展示结束会话按钮
    endSessionAction: Boolean(Number(data.basicSetting.isTakeSessionover)),
    // 机器人无效会话是否自动推送评价
    robotInvalid: Boolean(
      Number(data.satisfactionDTO.uneffectRobotSatisfaction),
    ),
    // 人工无效会话后是否推送满意度评价
    agentInvalid: Boolean(Number(data.satisfactionDTO.uneffectLaborSatisfaction)),
    // 自助服务状态
    selfService: Boolean(Number(data.basicSetting.selfService)),
    // 机器人拒识次数自动转人工
    robotRefuse: Number(data.systemConfiguration.ics_intervalDuration),
  }
  for (const key in systemValue) {
    handleSystemInfo(key, systemValue[key])
  }
  // 评价相关
  handleEvaluate(data['im.survey.level'], data)
  // 获取高频关键词
  const hotResponse: any = await requestHotKey({
    configId,
    tenantId: data.entranceDTO.tenantId,
  })
  hotWords.value = hotResponse.data
  return { hotWords, strongAuth: systemValue.isStrongAuth }
}
// 更新system数据
function handleSystemInfo(key: string, value: any) {
  const channel = useChannelStore()
  channel.updateSystemInfo({ key, value })
}
// 评价相关初始化
function handleEvaluate(level: Level[], config) {
  const channel = useChannelStore()
  const value = level.filter((item) => item.status === '1')
  const evaluate: any[] = value.map((item, index) => {
    return { total: value.length, text: item.name, active: index + 1 }
  })
  channel.setEvaluateRate(evaluate)
  const options = {
    lunReSolves: config.satisfactionDTO.lunReSolves,
    lunSaDics: config.satisfactionDTO.lunSaDics,
    runReSolves: config.satisfactionDTO.runReSolves,
    runSaDics: config.satisfactionDTO.runSaDics,
  }
  for (const key in options) {
    channel.setEvaluateSelect({
      key,
      value: options[key],
    })
  }
}
export default useChannel
